package cn.yl.fxboot.application.action;



import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import javafx.application.Platform;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;

public class MsgPaneActions{
	
	public static AnchorPane msgPane ;//= c.getMsgPane();
	
	public static TextField msgTpl_TF ;//= c.getMsgTpl_TF();
	
	public static void doLog(String msg) {
		Platform.runLater(()->{
			doLogLater(msg);
		});
	}
	
	private static Pane doLogLater(String msg) {
		TextField field = new TextField();
		field.setLayoutX(msgTpl_TF.getLayoutX());
		field.setLayoutY(msgPane.getChildren().size() * 30);
		field.setText(DateUtil.now() +"  " + msg);
		if(msg.contains("error:")) {
			String m = msg.replaceAll("error:", "错误:");
			field.setText(DateUtil.now() +"  " + m);
			field.setStyle("-fx-background-color:black;-fx-text-fill:red");
		}else if (msg.contains("success:")){
			String m = msg.replaceAll("success:", "");
			field.setText(DateUtil.now() +"  " + m);
			field.setStyle("-fx-background-color:black;-fx-text-fill:orange");
		}else {
			field.setStyle("-fx-background-color:black;-fx-text-fill:#16fc01");
		}
		field.setPrefWidth(msgTpl_TF.getPrefWidth());
		msgPane.getChildren().add(field);
		//field.requestFocus();
		Platform.runLater(() ->{
			//scroll();
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			scroll();
		});
		return msgPane;
	}
	
	public static void init(AnchorPane msgPaneSrc,TextField msgTpl_TFSrc) {
		msgPane = msgPaneSrc;
		msgTpl_TF = msgTpl_TFSrc;
	}
	
	private static void scroll() {
		Platform.runLater(() ->{
			ScrollPane sp = (ScrollPane) msgPane.getScene().lookup("#logScrollPane");
			sp.setVvalue(1.0);
		});
	}
	
	public static void clean() {
		msgPane.getChildren().clear();
	}
}
